﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class CustomerManage_TradeManage : System.Web.UI.Page
{
    #region 字段
    private string _callPhone = "";
    #endregion

    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["FCode"] = "400101";

        this._callPhone = SiteCommon.Common.GetString(Request.QueryString["CallPhone"]);

        if (!IsPostBack)
        {
            divOldButton.Visible = false;
            divOldSearch.Visible = false;
            this.Panel1.Visible = false;
            TextBox2.Attributes.Add("onclick", "SearchVIP('../');");
            ((Button)BUTTONDJLIST1.Controls[4]).Enabled = false;
            ((Button)BUTTONDJLIST1.Controls[6]).Enabled = false;
            ORDER_OFCODE();
            ORDER_DTCODE();
            //绑定支付方式
            List<Model.PAYTYPE> paytype = new List<Model.PAYTYPE>();
            paytype = new BLL.PAYTYPE().GetAllList();
            ddlPaylist.DataSource = paytype;
            ddlPaylist.DataTextField = "PT_NAME";
            ddlPaylist.DataValueField = "PT_CODE";
            ddlPaylist.DataBind();
            ListItem li = new ListItem();
            li.Text = "全部";
            li.Value = "-1";
            ddlPaylist.Items.Insert(0, li);

            Model.V_CUSTOMER user;
            BLL.UserInfoBL mUserInter = new BLL.UserInfoBL();
            string ret = mUserInter.GetUserByCallPhone(this._callPhone, out user);
            if (user != null)
            {
                txtEmail.Text = user.EMAIL;
                Bind();
            }

        }
    }
    private void ORDER_OFCODE()
    {
        List<Model.ORDERFROM> properties = new BLL.ORDERFROM().GetAllList();
        DropDownList1.DataSource = properties;
        DropDownList1.DataTextField = "OF_NAME";
        DropDownList1.DataValueField = "OF_CODE";
        DropDownList1.DataBind();
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        DropDownList1.Items.Insert(0, li);
    }
    private void ORDER_DTCODE()
    {
        List<Model.DEALTYPE> properties = new BLL.DEALTYPE().GetAllList();
        DropDownList2.DataSource = properties;
        DropDownList2.DataTextField = "DT_NAME";
        DropDownList2.DataValueField = "DT_CODE";
        DropDownList2.DataBind();
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        DropDownList2.Items.Insert(0, li);
    }
    private void Bind()
    {

        //根据支付方式查出相关的订单编号


        string email = txtEmail.Text.Trim();
        if (email == null
            || email.Trim() == "")
        {
            ORDERDOCListControl1.Visible = false;
            return;
        }

        BLL.UserInfoBL mUserInter = new BLL.UserInfoBL();
        string usercode = mUserInter.GetUserNameByEmail(email);
        if (usercode == null
            || usercode.Trim() == "")
        {
            ORDERDOCListControl1.Visible = false;
            return;
        }



        string sqlstr = " ";

        //sqlstr += " and order_customercode = '" + usercode + "' ";
        sqlstr += TextBox1.Text.Trim() == "" ? "" : " and order_id='" + TextBox1.Text.Trim() + "' ";
        //sqlstr += TextBox2.Text.Trim() == "" ? "" : " and order_customercode = '" + TextBox2.Text.Trim() + "' ";
        sqlstr += TextBox3.Text.Trim() == "" ? " and (order_createdate>'1900-01-01' " : " and (order_createdate>='" + TextBox3.Text.Trim() + "' ";
        sqlstr += TextBox4.Text.Trim() == "" ? " and order_createdate<'2099-01-01') " : " and order_createdate<='" + DateTime.Parse(TextBox4.Text.Trim()).AddDays(1).ToShortDateString() + "') ";
        sqlstr += DropDownList1.SelectedValue.Trim() == "-1" ? "" : " and ORDER_OFCODE='" + DropDownList1.SelectedValue + "' ";
        sqlstr += DropDownList2.SelectedValue.Trim() == "-1" ? "" : " and PD_DTCODE='" + DropDownList2.SelectedValue + "' ";
        sqlstr += CheckBox1.SelectedValue == "-1" ? "" : " and ORDER_CHECK=" + Int32.Parse(CheckBox1.SelectedValue) + " ";
        sqlstr += CheckBox2.SelectedValue == "-1" ? "" : " and ORDER_ACHECK=" + Int32.Parse(CheckBox2.SelectedValue) + " ";
        sqlstr += CheckBox3.SelectedValue == "-1" ? "" : " and ORDER_DELETE=" + Int32.Parse(CheckBox3.SelectedValue) + " ";
        sqlstr += CheckBox4.SelectedValue == "-1" ? "" : " and ORDER_SEND=" + Int32.Parse(CheckBox4.SelectedValue) + " ";
        sqlstr += CheckBox5.SelectedValue == "-1" ? "" : " and ORDER_LOCK=" + Int32.Parse(CheckBox5.SelectedValue) + " ";
        sqlstr += dropClear.SelectedValue == "-1" ? "" : " and ORDER_CLEAR=" + Int32.Parse(dropClear.SelectedValue) + " ";
        sqlstr += ddlPaylist.SelectedValue.Trim() == "-1" ? "" : " and PD_PAYTYPE='" + ddlPaylist.SelectedValue + "' ";
        sqlstr += DropDownList3.SelectedValue.Trim() == "-1" ? "" : " and ORDER_SECDEAL=" + DropDownList3.SelectedValue + " ";
        sqlstr += DropDownList4.SelectedValue.Trim() == "-1" ? "" : " and ORDER_APAY=" + DropDownList4.SelectedValue + " ";

        sqlstr += txtgddh.Text.Trim() == "" ? "" : " and ORDER_GETERPHONE1='" + txtgddh.Text.Trim() + "' ";
        sqlstr += txtsj.Text.Trim() == "" ? "" : " and ORDER_GETERPHONE2='" + txtsj.Text.Trim() + "'  ";
        sqlstr += txtaddress.Text.Trim() == "" ? "" : " and ORDER_GETADDRESS='" + txtaddress.Text.Trim() + "'  ";
        sqlstr += txtspdm.Text.Trim() == "" ? "" : " and OD_CMDCODE='" + txtspdm.Text.Trim() + "' ";

        int pageCount, rowCount;
        //DataSet properties = new BLL.ORDERDOC().GetList2("orderdoc left join PAYDETAIL on ORDER_ID=PD_ID  left join ORDERDETAIL on order_id=od_id ", "orderdoc.*", false, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "ORDER_CREATEDATE", true, sqlstr, out pageCount, out rowCount);
        DataSet properties = new BLL.ORDERDOC().P_GetOrderList_OrderBy(AspNetPager1.StartRecordIndex, AspNetPager1.PageSize, sqlstr, "order_createdate");


        rowCount = Int32.Parse(properties.Tables[1].Rows[0][0].ToString());
        pageCount = rowCount / AspNetPager1.PageSize;

        ORDERDOCListControl1.Visible = true;
        ORDERDOCListControl1.Bind(properties.Tables[0]);
        AspNetPager1.RecordCount = rowCount;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        Bind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
        //int pageCount, rowCount;
        //List<Model.ORDERDOC> properties = new BLL.ORDERDOC().GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "ORDER_ID", false, "", out pageCount, out rowCount);
        //ORDERDOCListControl1.Bind(colors);
    }
    public void Add()
    {
        if (Check.CheckPermission("400101", "00001"))
        {
            Response.Redirect("NewOrder.aspx");
        }
        else
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
    }
    public void Update()
    {
        if (Check.CheckPermission("400101", "00002"))
        {
            List<string> s = ORDERDOCListControl1.Selected;
            if (s.Count > 0)
            {
                Response.Redirect("OrderDetail.aspx?code=" + s[0]);
            }
            else
            {
                Share.Alert(this.Page, "请选择一条需要编辑的记录"); return;
            }
        }
        else
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
    }

    protected void btngjcx_Click(object sender, EventArgs e)
    {


    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (this.Panel1.Visible == false)
        {
            this.Panel1.Visible = true;
            Button3.Text = "<<";

        }
        else
        {
            this.Panel1.Visible = false;
            Button3.Text = ">>";

        }


    }
}
